package com.ecarx.camera.ui.dialog;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatImageButton;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.ecarx.camera.R;
import com.ecarx.camera.constant.Constants;
import com.ecarx.camera.utils.Utils;

public class SelectionDialogAdapter extends RecyclerView.Adapter<SelectionDialogAdapter.ViewHolder> {

    @Constants.SelectionType
    private String type;
    private CharSequence[] txts;
    private boolean[] selectedStatus;
    private OnItemClickListener onItemClickListener;

    public void setDataSource(@Constants.SelectionType String type, @NonNull CharSequence[] txts, @NonNull boolean[] selectedStatus) {
        this.type = type;
        this.txts = txts;
        this.selectedStatus = selectedStatus;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(Utils.getApp()).inflate(R.layout.layout_dialog_private_policy_consent_period_rv_selection_item, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        int adapterPosition = holder.getAdapterPosition();
        holder.textView.setText(txts[adapterPosition]);
        holder.textView.setTextColor(ContextCompat.getColor(Utils.getApp(), R.color.camera_base_day_night_fg_color));
        holder.ibtnSelect.setImageDrawable(ContextCompat.getDrawable(Utils.getApp(), selectedStatus[adapterPosition] ? R.drawable.camera_base_ic_check_box_checked : R.drawable.camera_base_ic_check_box_unchecked));
        if (Constants.SelectionType.SINGLE.equals(type)) {
            holder.ibtnSelect.setEnabled(!selectedStatus[adapterPosition]);
        }
        if (onItemClickListener != null) {
            holder.ibtnSelect.setOnClickListener(v -> onItemClickListener.onItemClick(adapterPosition, !selectedStatus[adapterPosition]));
        }
        holder.viewDivideLine.setBackgroundColor(ContextCompat.getColor(Utils.getApp(), R.color.camera_base_view_divide_line_bg_color));
    }

    @Override
    public int getItemCount() {
        return txts == null ? 0 : txts.length;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        public TextView textView;
        public AppCompatImageButton ibtnSelect;
        public View viewDivideLine;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.text_view);
            ibtnSelect = itemView.findViewById(R.id.ibtn_select);
            viewDivideLine = itemView.findViewById(R.id.view_divide_line);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(int position, boolean selected);
    }
}
